
package sr.codemg.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sr.codemg.config.JwtConfig;
import sr.codemg.entity.SysMenus;
import sr.codemg.service.SysMenusService;
import sr.codemg.utils.Constants;
import sr.codemg.vm.Base;
import sr.codemg.vm.Menus;
import sr.codemg.vm.Selects;
import sr.codemg.vm.Tables;

import java.util.List;
import java.util.Map;

/**
 * 菜单信息
 **/
@Slf4j
@RestController
@RequestMapping("/sysmenus")
public class SysMenusController {

    @Autowired
    SysMenusService sysMenusService;

    @GetMapping("/{id}")
    public SysMenus get(@PathVariable("id") String id) {
        return sysMenusService.get(id);
    }

    @PutMapping
    public Tables list(@RequestBody Map<String, Object> params) {
        return sysMenusService.findList(params);
    }

    @PostMapping
    public Base.Response post(@RequestBody SysMenus entity) {
        entity.setId(null);
        return sysMenusService.add(entity);
    }

    @PutMapping("/edit")
    public Base.Response put(@RequestBody SysMenus entity) {
        return sysMenusService.update(entity);
    }

    @DeleteMapping("/{id}")
    public Base.Response delete(@PathVariable("id") String id) {
        return sysMenusService.delete(id);
    }

    @PostMapping("/select")
    public Selects.Response select(@RequestBody Selects.Request req) {
        return sysMenusService.select(req);
    }

    @GetMapping("/home")
    public List<Menus> home(@CookieValue(Constants.Authorization) String token) {
        return sysMenusService.home(JwtConfig.thinning(token));
    }

    @GetMapping("/roles")
    public List<Menus> roles(@CookieValue(Constants.Authorization) String token) {
        return sysMenusService.roles(JwtConfig.getUserId(token));
    }
}

